<?php

class Reale_Model_EnquiryMapper 
{
	/**
     * @var Zend_Db_Table_Abstract
     */
    protected $_dbTable;

    /**
     * Specify Zend_Db_Table instance to use for data operations
     * 
     * @param  Zend_Db_Table_Abstract $dbTable 
     * @return Reale_Model_EnquiryMapper
     */
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    /**
     * Get registered Zend_Db_Table instance
     *
     * Lazy loads Reale_Model_DbTable_Enquiry if no instance registered
     * 
     * @return Zend_Db_Table_Abstract
     */
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Reale_Model_DbTable_Enquiry');
        }
        return $this->_dbTable;
    }
    
 	/**
     * 
     * To put new enquiry. Enquiry is always inserted not updated. 
     * @param Reale_Model_Enquiry
     * @return Reale_Model_EnquiryMapper | null 
     */
    public function newEnquiry(Reale_Model_Enquiry $enquiry){
    	$data = array(
    		'agent_id' => $enquiry->getAgent_id(),
    		'subject' => $enquiry->getSubject(),
    		'body'=> $enquiry->getBody()
        );

        if (null === ($id = $enquiry->getEnquiry_id())) {
            unset($data['id']);
            $this->getDbTable()->insert($data);
            return this;
        }
        
        return null;
    }
	
}

